/**
 * Created by Administrator on 2017/3/1 0001.
 */
/**
 * 计算每天增加人数
 * @param id {string}
 * @param str {string}
 */
(function countPeople(id, str) {
  var arr = str.split(/[- : \/]/), date = new Date(arr[0], arr[1] - 1, arr[2], arr[3], arr[4], arr[5]);
  var num = parseInt((new Date() - date) / (60 * 60 * 24 * 2));
  document.getElementById(id).innerHTML = num;
})('peopleNum', '2017/3/7 11:11:11');

/**
 * 抽奖滚动
 * */
(function rolling(rollData) {
  var $ul = $('#rollBox').find(rollData.ul);
  var $wrap = $ul.parent();
  var $liAll = $ul.find(rollData.li);
  var moveLength = $liAll.height();
  var liNumbers = $liAll.children().length;
  var repeatNumber = Math.ceil($wrap.height() / $liAll.height());
  var clones = $ul.find("li:lt(" + repeatNumber + ")").clone();

  $ul.append(clones);

  $ul.css({
    '-webkit-transition': 'all ' + rollData.speed + 'ms linear',
    'transition': 'all ' + rollData.speed + 'ms linear',
    '-webkit-transform': 'translateY(' + moveLength * ( -liNumbers) + 'px)',
    'transform': 'translateY(' + moveLength * ( -liNumbers) + 'px)'
  });

  $ul.on('transitionend webkitTransitionEnd', function () {
    $ul.css({
      '-webkit-transition': '',
      'transition': '',
      'transform': '',
      '-webkit-transform': ''
    });
    setTimeout(function () {
      $ul.css({
        '-webkit-transition': 'all ' + rollData.speed + 'ms linear',
        'transition': 'all ' + rollData.speed + 'ms linear',
        '-webkit-transform': 'translateY(' + moveLength * ( -liNumbers) + 'px)',
        'transform': 'translateY(' + moveLength * ( -liNumbers) + 'px)'
      });
    })
  });
})({
  ul: 'ul',
  li: 'li',
  speed: 15000
});


var state = {
  chance: 1
  , phone: ''
  , code: ''
};
var regPhone = /^1[3|4|5|7|8][0-9]\d{8}$/;
var regCode = /[^\s]+/;
var prizes = [
  {
    title: '奖项一'
    , content: '免费尊享一对一投顾服务（15天）'
  }
  , {
    title: '奖项二'
    , content: '免费获布局牛股一只'
  }
  , {
    title: '奖项三'
    , content: '次月君证研究所调研计划'
  }
  , {
    title: '奖项四'
    , content: 'VIP短线宝一周服务'
  }
  , {
    title: '奖项五'
    , content: '1000元抵1000元投顾抵用券'
  }
  , {
    title: '奖项六'
    , content: '直播室资深讲师战法课件一份'
  }
];

function lottery() {
  var num;
  if(typeof window.localStorage==='object'){
    (function(){
      if(localStorage.getItem('prize')){
        num=localStorage.getItem('prize');
      }else{
        num = Math.floor(Math.random() * prizes.length);
        localStorage.setItem('prize',num);
      }
    }())
  }else{
    (function(){
      var str;
      var arr;
      var exdate;
      var index;
      if (document.cookie === "") {
        num = Math.floor(Math.random() * prizes.length);
        exdate = new Date();
        exdate.setDate(exdate.getDate() + 60);
        document.cookie = 'prize=' + num + ';expires=' + exdate.toGMTString();

      } else {
        str = document.cookie;
        arr = str.split('=');
        index = str.indexOf('prize=');

        console.log(str);
        console.log(arr);
        num=str.slice(index + 6, index + 7);
      }
    }());
  }
  state.prize = prizes[num];
}

var $submit = $('#submit');
$submit.find('.choice').click(function () {
  var $this = $(this);
  if ($this.hasClass('on')) {
    return
  }
  else {
    $submit.find('.choice.on').removeClass('on').next().removeClass('active').attr('disabled', 'disabled');
    $(this).addClass('on').next().addClass('active').removeAttr('disabled');
  }
});
$('#code').keyup(function () {
  state.code = this.value;
  if (regCode.test(state.code)) {
    $('#submitBtn').removeClass('cannot').addClass('submitBtn');
  } else {
    $('#submitBtn').removeClass('submitBtn').addClass('cannot');
  }

});
$('#phone').keyup(function () {
  state.phone = this.value;
  if (regPhone.test(state.phone)) {
    $('#submitBtn').removeClass('cannot').addClass('submitBtn');
  } else {
    $('#submitBtn').removeClass('submitBtn').addClass('cannot');
  }
});
$('#submitBtn').click(function () {
  if ($(this).html() == '点击关闭') {
    $('#pop').hide();
    return;
  }
  if (regPhone.test(state.phone) || regCode.test(state.code)) {
    $.ajax({
      url: ctx + 'toZeroOrder/ZeroToOrder3',
      type: "post",
      data: {
        "phone": state.phone,
        "tickets": state.code,
        "prizes": state.prize.content,
        "way": "直播室活动"
      },
      async: true,
      success: function (datas) {
        if (datas.flag) {
          $('#submitBox').html('您的信息已经提交成功');
          $('#submitBtn').html('点击关闭');
          $('#prize').find('.second').html(' ');
          $('#prize').find('.third').html(' ');
        }
        else {
          alert("亲！您已经领取过奖品，不可重复领取 ~");
        }
      },
      error: function () {
        alert("连接失败，请稍后再试！");
      }
    });
  } else {
    if ($('#codeC').hasClass('on')) {
      alert('请输入正确的入场券');
    } else if ($('#phoneC').hasClass('on')) {
      alert('请输入正确的手机号码');
    } else {
      alert('请输入正确的格式信息');
    }
  }
});
$('#smash').find('.left').click(function () {
  if (state.chance == 1) {
    state.chance--;
    lottery();
    $('#prize').find('.prizeNum').html(state.prize.title);
  }
  $('#pop').show();
});
